// @flow
import React, { Component } from 'react'
import { Table, Input, Button, Icon, Checkbox } from 'antd'
const CheckboxGroup = Checkbox.Group
import styles from './CustomerTable.css'
import R from 'ramda'

type Props = {
  rowData: Array<Object>,
  refreshCustomers: Function,
  customersPermission: Object,
  history: Object
}
type States = {
  columnDefs: Array<Object>,
  filterDropdownVisible: boolean,
  rowData: Array<Object>,
  searchText: string,
  filtered: boolean,
}
class CustomerGrid extends Component {
  props: Props
  state: States
  onChange222: Function
  onChange222Customer: Function
  constructor (props: Props) {
    super(props)
    // const component = this
    this.state = {
      columnDefs: [],
      filterDropdownVisible: false,
      rowData: this.props.rowData,
      searchText: '',
      filtered: false
    }
    this.onChange222 = this.onChange222.bind(this)
    this.onChange222Customer = this.onChange222Customer.bind(this)
  }

  componentWillReceiveProps (nextProps: Props) {
    // console.log('will改变-----------------');
    const component = this
    const { customersPermission } = nextProps
    this.setState({
      columnDefs: [
        {
          title: '客户编号',
          dataIndex: 'customerNo',
          width: '10%'
        },
        {
          title: '客户名称',
          dataIndex: 'name',
          width: '40%'
        },
        {
          title: '所属行业',
          dataIndex: 'industry',
          width: '10%'
        },
        {
          title: '所属地区',
          dataIndex: 'region.location',
          width: '10%'
        },
        {
          title: '权限',
          dataIndex: 'id',
          width: '30%',
          render: data => <CheckboxGroup
            value={customersPermission[data] ? customersPermission[data].permission : []}
            onChange={function (value) { component.onChange222(value, data) }}
            options={[
              { label: '查看', value: 'readPermission' },
              { label: '编辑', value: 'editPermission' },
              { label: '删除', value: 'delPermission' }
            ]} />
        }
      ]
    })
  }

  onChange222 (checkedValues: Array<string>, data: string) {
    const { customersPermission } = this.props
    // 编辑或删除选中后需选中“查看”
    if (checkedValues.length > 0 && checkedValues.indexOf('readPermission') === -1) {
      checkedValues.push('readPermission')
    }
    const obj = {
      id: data,
      permission: checkedValues
    }
    const customer = Object.assign({}, customersPermission, {
      [data]: obj
    })
    this.props.refreshCustomers(customer)
  }
  onChange222Customer (data: Object) {

  }
  render () {
    const { columnDefs } = this.state
    return (
      <div className={styles['tableCon']}>
        <Table
          pagination={false}
          scroll={{ y: 300 }}
          columns={columnDefs} dataSource={this.props.rowData} />
      </div>
    )
  }
}

export default CustomerGrid
